﻿using System.Collections.Generic;
using KC.Common;
using KC.Framework.Tenant;
using KC.DTO.Account;
using KC.Service.Core.DTO.Message;

namespace KC.Service.Core.WebApiService.Business
{
    public class MessageApiService : IdSrvOAuth2ClientRequestBase, IMessageApiService
    {
        private const string ServiceName = "KC.Service.Core.WebApiService.MessageApiService";

        public MessageApiService(
            Tenant tenant, 
            System.Net.Http.IHttpClientFactory httpClient,
            Microsoft.Extensions.Logging.ILogger<MessageApiService> logger)
            : base(tenant, httpClient, logger)
        {
        }

        public bool AddMessageLog(MessageLogDTO data)
        {
            var postData = SerializeHelper.ToJson(data);
            ServiceResult<bool> result = null;
            WebSendPost<ServiceResult<bool>>(
                ServiceName + ".AddMessageLog",
                SsoApiServerUrl + "MessageApi/AddMessageLog",
                ApplicationConstant.SsoScope,
                postData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success)
            {
                return result.Result;
            }

            return false;
        }

        public MessageClassDTO GetMessageClassByCode(string code)
        {
            ServiceResult<MessageClassDTO> result = null;
            WebSendGet<ServiceResult<MessageClassDTO>>(
                ServiceName + ".GetMessageClassByCode",
                SsoApiServerUrl + "MessageApi/GetMessageClassByCode?code=" + code,
                ApplicationConstant.SsoScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<MessageClassDTO>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        public MessageClassDTO GetMessageClassByName(string name)
        {
            ServiceResult<MessageClassDTO> result = null;
            WebSendGet<ServiceResult<MessageClassDTO>>(
                ServiceName + ".GetMessageClassByName",
                SsoApiServerUrl + "MessageApi/GetMessageClassByName?name=" + name,
                ApplicationConstant.SsoScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<MessageClassDTO>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }

        public bool AddRemindMessage(RemindMessageApiDTO data)
        {
            var postData = SerializeHelper.ToJson(data);
            ServiceResult<bool> result = null;
            WebSendPost<ServiceResult<bool>>(
                ServiceName + ".AddRemindMessage",
                SsoApiServerUrl + "MessageApi/AddRemindMessage",
                ApplicationConstant.SsoScope,
                postData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success)
            {
                return result.Result;
            }

            return false;
        }

        public List<UserDTO> LoadUserDefaulAll(List<string> userIds=null)
        {
            ServiceResult<List<UserDTO>> result = null;
            WebSendPost<ServiceResult<List<UserDTO>>>(
                ServiceName + ".LoadUserDefaulAll",
                SsoApiServerUrl + "MessageApi/LoadUserDefaulAll",
                ApplicationConstant.SsoScope,
                SerializeHelper.ToJson(userIds),
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                });
            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
    }
}
